package com.example.haohao.myview.expandView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.haohao.myview.R;

import java.util.ArrayList;

public class MyExpandActivity extends AppCompatActivity {

    private LinearLayout expandView_ll;
    private TextView expandView_tv;
    private ImageView expandView_iv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_expand);
        expandView_ll = (LinearLayout) findViewById(R.id.ll_expand_ll);
        expandView_tv = (TextView) findViewById(R.id.ll_expand_tv);
        expandView_iv = (ImageView) findViewById(R.id.ll_expand_iv);

        expandView_ll.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {

                // popupwindow
                showPopwindow();
//                img
                expandView_iv.setImageDrawable(getDrawable(R.drawable.img_up));


            }
        });

    }

    private void showPopwindow() {
        View parent = ((ViewGroup) this.findViewById(android.R.id.content));
        View popView = View.inflate(this, R.layout.camera_pop_menu, null);

        ImageView btnCamera = (ImageView) popView.findViewById(R.id.img_close);


        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;


        final PopupWindow popWindow = new PopupWindow(popView,width,height);

        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置同意在外点击消失

        RecyclerView   rl = (RecyclerView) popView.findViewById(R.id.rl);

        final ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            strings.add("theyyours" + i + "e");
        }

        MyAdapter adapter = new MyAdapter(popView.getContext(), strings);

        LinearLayoutManager layoutManager = new LinearLayoutManager(popView.getContext()) ;

        rl.setLayoutManager(layoutManager);

        rl.setAdapter(adapter);

        adapter.setOnClick(new MyAdapter.PassPosition() {
            @SuppressLint("NewApi")
            @Override
            public void pass(int position) {
                popWindow.dismiss();
                expandView_iv.setImageDrawable(getDrawable(R.drawable.img_down));
                expandView_tv.setText(returnString(strings.get(position)));
            }
        });

        btnCamera.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
                expandView_iv.setImageDrawable(getDrawable(R.drawable.img_down));
                expandView_tv.setText("shoot");
            }
        });

        popWindow.setBackgroundDrawable(new ColorDrawable(0x30000000));
        popWindow.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    String returnString(String str){
        if (str.length()>10) {
            String strHead,strEnd;
            strEnd=str.substring(str.length()-10);
            strHead=str.substring(0,str.length()-10);
           return strHead+getStarString(strEnd,3,7);
        }
        return "";
    }
    /**
     * 对字符串处理:将指定位置到指定位置的字符以星号代替
     *
     * @param content
     *            传入的字符串
     * @param begin
     *            开始位置
     * @param end
     *            结束位置
     * @return
     */
    private static String getStarString(String content, int begin, int end) {

        if (begin >= content.length() || begin < 0) {
            return content;
        }
        if (end >= content.length() || end < 0) {
            return content;
        }
        if (begin >= end) {
            return content;
        }
        String starStr = "";
        for (int i = begin; i < end; i++) {
            starStr = starStr + "*";
        }
        return content.substring(0, begin) + starStr + content.substring(end, content.length());

    }
}